//index.js
//获取应用实例
const app = getApp();
let navNews = []; 
let newsCon = [];
let newShow = [];
let newsMore = [];
let daoH =[];
let pages = 1;
let page = 1;
let count = 0;
var formatTime = require('../../utils/util.js');

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    activeId: 0,
    newsCon: [],
    navNews: [],
    newShow: [],
    newsMore: [],
    timeList:[],
    daoH: [],
    page: 1,
    pages: 1,
    articles: [],
    pagenumber: 0,
    count:0,
    navList: [
      {
        nav: '头条'
      },
      {
        nav: '财经'
      },
      {
        nav: '科技'
      },
      {
        nav: '体育'
      },
      {
        nav: '娱乐'
      },
      {
        nav: '教育'
      },
      {
        nav: '军事'
      },
      {
        nav: '股票'
      },
      {
        nav: '健康'
      }
    ],
    // 加载的三个参数
    hasMoreData: true,
    isRefreshing: false,
    isLoadingMoreData: false,

    // backTopValue: false
  },
  
  

  onLoad: function(e){
    // 初始化导航
    var that = this;
    const requestTask = wx.request({
      url: 'https://www.dearloving.cn/wx.php',
      dataType:'JSON',
      data:{},
      method: "GET",
      header: {
        'content-type': 'application/json'
      },
      success(res){
        var listArr = JSON.parse(res.data);
        that.setData({
          navNews: listArr,
        })
      }
    });
    // 初始化刚进入页面的第一个页面
    wx.request({
      url: 'https://www.dearloving.cn/news.php',
      dataType: 'JSON',
      data: {},
      method: "GET",
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        var contArr = JSON.parse(res.data);
        contArr.forEach((v,i)=>{
          v.news_time = formatTime.formatTime(v.news_time, 'Y/M/D h:m:s')
          if(v.news_pic == null){
            v.news_pic = './uploads/timg.jpg'
          }
        });
        that.setData({
          newsCon: contArr
        })
      }
    });
    // 查询总数
    wx.request({
      url: 'https://www.dearloving.cn/count.php',
      dataType: 'JSON',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        count = res.data;
        pages = (count / 20).toFixed(0);
      }
    })
  },

  //监听滚动 暂时用不到 
  onPageScroll: function (e) {
    
    var that = this;
    var scrollTop = e.scrollTop;
    var backTopValue = scrollTop > 500 ? true : false;
    that.setData({
      backTopValue: backTopValue,
    })
  },

  // 导航点击切换新闻类型
  navi: function (e) {
    var that = this;

    
    let name_list = e.currentTarget.dataset.href;
    page = 1;
    // wx.pageScrollTo({
    //   scrollTop: 0
    // })
    that.setData({
      activeId: e.currentTarget.id,
      pagenumber: e.currentTarget.id,
      newsMore: [],
      page: 1,
    });

    that.setData({
      hasMoreData: true,
      isLoadingMoreData: false
    })
    
    
    
    // wx.request({
    //   url: 'https://www.dearloving.cn/nr.php',
    // })

    /* 天行数据接口*/
    // wx.request({
    //   url: 'http://api.tianapi.com/' + name_list +'/?key=5d16123513aea5a71414b8ec700ba834',
    //   method:  "GET",
    //   dataType: "json",
    //   data: {},
    //   header: {
    //     "Content-Type": "application/x-www-form-urlencoded"
    //   },
    //   success(res){
    //     console.log(res.data.newslist);
    //   },

    // }),

    /* 急速数据接口 */

    // wx.request({
    //   url: 'http://api.jisuapi.com/news/get?channel=' + name_list + '&start=0&num=20&appkey=584c609c551bb659',
    //   method: "GET",
    //   dataType: "json",
    //   data: {},
    //   header: {
    //     "Content-Type": "application/x-www-form-urlencoded"
    //   },
    //   success(res) {
    //     var newsList = res.data.result.list;
    //     that.setData({
    //       newShow: newsList
    //     });
    //   },

    // })
    
    var news_cate_id = e.currentTarget.dataset.id;
    wx.request({
      url: 'https://www.dearloving.cn/daohang.php',
      dataType: 'JSON',
      data: {
        news_cate_id: news_cate_id,
      },
      method: "GET",
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        var contArr = JSON.parse(res.data);
        contArr.forEach((v, i) => {
          v.news_time = formatTime.formatTime(v.news_time, 'Y/M/D h:m:s')
          if (v.news_pic == null) {
            v.news_pic = './uploads/timg.jpg'
          }
        });
        that.setData({
          newShow: contArr
        });
      }
    })

  },
  // 跳转详情页
  pageShow:function(e){
    var pageId = e;
    wx.setStorage({
      key: 'news_id',
      data: e.currentTarget.dataset.id,
    })
    wx.navigateTo({
      url: '/pages/pageshow/pageshow?newsId=' + e.currentTarget.dataset.id,
    })
  },

  // 滚动加载
  onReachBottom(e) {
    var that = this;
    // if (this.data.isRefreshing || this.data.isLoadingMoreData || !this.data.hasMoreData) {
    //   return
    // };


    this.setData({
      isLoadingMoreData: true,
    });

    var cateId = that.data.activeId;
    
    wx.request({
      url: 'https://www.dearloving.cn/sx.php',
      dataType: 'JSON',
      data: {
        page: page,
        cateId: cateId
      },
      method: "GET",
      header: {
        'content-type': 'application/json'
      },
      success(res) {

        var contArr = JSON.parse(res.data);

        contArr.forEach((v, i) => {
          v.news_time = formatTime.formatTime(v.news_time, 'Y/M/D h:m:s')
          if (v.news_pic == null) {
            v.news_pic = './uploads/timg.jpg'
          }
        });
        
        if(page < pages){
          page++;
        }

        if(page ==  pages){
          that.setData({
            hasMoreData: false,
            isLoadingMoreData: false
          })
        }
        

        that.setData({
          newsMore: contArr,
          isLoadingMoreData: false
        });
        

        if (contArr.length == 0) {
          that.setData({
            hasMoreData: false,
            isLoadingMoreData: false
          })
        };
        
      },
      fail(erroy){
        console.log(erroy);
      }

    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  pagesIndex: function(){
    wx.navigateTo({
      url: '../index/index'
    });
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
